<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function overview_list_page()
{
    @$output = drupal_render(drupal_get_form('overview_list_form'));
    return $output;
}

function overview_list_form($form, &$form_state)
{
    global $user;
    if (!empty($_GET['farmer_id'])) {
        $form_state['farmer'] = $_GET['farmer_id'];
    }
    if(!empty($form_state['farmer'])){
        $form['#farmer'] = $form_state['farmer'];
    }
    $form['farmer_info']['#markup'] = data_list_view_page($form, $form_state);
//    ajax_submit_form($form, $form_state, $table, '');
//    echo '<pre>';
//    print_r($form['farmer_info']);die;
    return $form;
}
function data_list_view_page($form = '', &$form_state = '')
{
    drupal_add_js ( drupal_get_path ( 'module', 'farmer' ) . '/js/farmer.overview.js' );
    global $user;
    $user_info = _get_user_info($user->uid);
    //农户信息
    $farmer_info = nonghua_data_load('farmer', 'farmer_id', $form_state['farmer']);
    if(!empty($farmer_info->distributor_id)){
        $query=db_select('distributor','D');
        $query->condition('D.distributor_id',$farmer_info->distributor_id,'=');
        $query->addField('D','distributor_name');
        $distributor_name = $query->execute()->fetchObject();
        $farmer_info->distributor_name = $distributor_name->distributor_name;
    }else{
        $farmer_info->distributor_name ="";
    }
    if(!empty($farmer_info->addr_id)){
        $addr_name = get_all_addrname($farmer_info->addr_id);
        $farmer_info->addr_name = $addr_name;
    }else{
        $farmer_info->addr_name ="";
    }
    //种植信息
    $query = db_select('plant_info', 'PI');
    $query->join('farmer', 'F', 'F.farmer_id=PI.farmer_id');
    $query->join('plant', 'P', 'P.plant_id=PI.plant_id');
    $query->join('addr', 'A', 'F.addr_id=A.addr_id');

    if (get_user_role_name($user->uid) === '农户') {
        $query->condition('PI.farmer_id', $user->uid);
    } else {
        if (!empty($_GET['farmer_id'])) {
            $query->condition('PI.farmer_id', $_GET['farmer_id']);
            $form_state['farmer'] = $_GET['farmer_id'];
        }
        if (!empty($form_state['farmer'])) {//
            $query->condition('PI.farmer_id', $form_state['farmer'], '=');
        }
        if(!user_access('view all plant_info') && user_access('view own plant_info')){
            $query->condition('PI.author_uid',$user->uid, '=');
        }
    }
    $query->fields('PI');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->leftJoin('addr', 'A4', 'A.town_id=A4.addr_id');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('A4', 'name', 'town');
    $query->addField('A', 'name', 'village');
    $query->addField('P', 'plant_name');
    $query->addField('F', 'farmer_name');
    $query->addField('F', 'farmer_id');
    $query->addField("PI",'demonstration_area');
    $query->addField("PI",'sacf_fertilizer');
    $query->addField("PI",'sacf');
    $query->orderBy('PI.timestamp', 'DESC');
    $query->range(0,20);
    $plant_infoInfo = $query->execute()->fetchAll();



    // 土壤信息
    $query = db_select('soil_info', 'I');
    $query->leftjoin('farmer', 'F', 'F.farmer_id=I.farmer_id');
    $query->fields('I');
    $query->addField('F', 'farmer_name');
    $query->addField('F', 'addr_id');
    $query->join('addr', 'A', 'A.addr_id=F.addr_id');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->leftJoin('addr', 'A4', 'A.town_id=A4.addr_id');

    if ($user_info['role'] === '农户') {
        $query->condition('I.farmer_id', $user->uid);
    } else {
        if (!empty($_GET['farmer_id'])) {
            $query->condition('I.farmer_id', $_GET['farmer_id']);
            $form_state['farmer'] = $_GET['farmer_id'];
        }
        if (!empty($form_state['farmer'])) {//
            $query->condition('I.farmer_id', $form_state['farmer'], '=');
        }
        if(!user_access('view all soil_info') && user_access('view own soil_info')){
            $query->condition('I.author_uid',$user->uid, '=');
        }
    }
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('A4', 'name', 'town');
    $query->addField('A', 'name', 'village');
    $query->addField('F', 'farmer_id');
    $query->orderBy('I.timestamp', 'DESC');
    $query->range(0,20);
    $soil_infoInfo = $query->execute()->fetchAll();


    //礼物信息
    $query = db_select('gift', 'G');
    $query->join('farmer', 'F', 'G.farmer_id = F.farmer_id');
    $query->join('addr', 'A', 'A.addr_id=F.addr_id');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->leftJoin('addr', 'A4', 'A.town_id=A4.addr_id');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('A4', 'name', 'town');
    $query->addField('A', 'name', 'village');
    //模糊条件筛选
    if ($user_info['role'] == '农户') {
        $query->condition('G.farmer_id', $user->uid, '=');
    }
    if (!empty($_GET['farmer_id'])) {
        $query->condition('G.farmer_id', $_GET['farmer_id']);
        $form_state['farmer'] = $_GET['farmer_id'];
    }
    if (!empty($form_state['farmer'])) {
        $query->condition('G.farmer_id', $form_state['farmer']);
    }
    $query->fields('G');
    $query->fields('F', array('farmer_id', 'farmer_name'));
    $query->orderBy('G.timestamp', 'DESC');
    $query->range(0,20);
    $giftInfo = $query->execute()->fetchAll();

    //施肥案例
    $query = db_select('fertilizer_case', 'C');
    $query->join('farmer', 'F', 'C.farmer_id=F.farmer_id');
    $query->join('plant', 'P', 'C.plant_id=P.plant_id');
    $query->join('addr', 'A', 'A.addr_id=F.addr_id');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->leftJoin('addr', 'A4', 'A.town_id=A4.addr_id');
    $query->leftJoin('addr', 'A5', 'A.village_id=A5.addr_id');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('A4', 'name', 'town');
    $query->addField('A5', 'name', 'village');
    $query->fields('C');
    $query->addField('F', 'farmer_name');
    $query->addField('P', 'plant_name');
    $query->orderBy('C.timestamp', 'DESC');
    if(!empty($_GET['farmer_id'])){
        $query->condition('C.farmer_id',$_GET['farmer_id']);
        $form_state['farmer'] = $_GET['farmer_id'];
    }
    if (!empty($form_state['farmer'])) {//
        $query->condition('C.farmer_id', $form_state['farmer'], '=');
    }
    if ($user_info['role'] != '农户') {
        if (!user_access('view all fertilizer_case') && user_access('view own fertilizer_case')) {
            $query->condition('C.author_uid', $user->uid, '=');
        }
    }
    $query->range(0,20);
    $fertilizer_caseInfo = $query->execute()->fetchAll();


    //回访记录
    $query = db_select('revisit', 'R');
    $query->join('farmer', 'F', 'F.farmer_id=R.farmer_id');
    $query->join('plant', 'P', 'P.plant_id=R.plant_id');
    $query->join('addr', 'A', 'A.addr_id=F.addr_id');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->leftJoin('addr', 'A4', 'A.town_id=A4.addr_id');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('A4', 'name', 'town');
    $query->addField('A', 'name', 'village');
    if(!empty($_GET['farmer_id'])){
        $query->condition('R.farmer_id',$_GET['farmer_id']);
        $form_state['farmer'] = $_GET['farmer_id'];
    }
    if (!empty($form_state['farmer'])) {//
        $query->condition('R.farmer_id', $form_state['farmer'], '=');
    }
    if ($user_info['role'] != '农户') {
        if (!user_access('view all revisit') && user_access('view own revisit')) {
            $query->condition('R.author_uid', $user->uid, '=');
        }
    }
    $query->fields('R');
    $query->addField('F', 'farmer_name');
    $query->addField('F', 'addr_id');
    $query->addField('P', 'plant_name');
    $query->orderBy('R.timestamp', 'DESC');
    $query->range(0,20);
    $revisitInfo = $query->execute()->fetchAll();

    //田间测产
    $query = db_select('plant_measure', 'PM');
    $query->join('farmer', 'F', 'PM.farmer_id=F.farmer_id');
    $query->join('plant', 'P', 'PM.plant_id=P.plant_id');
    $query->join('addr', 'A', 'A.addr_id=F.addr_id');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->leftJoin('addr', 'A4', 'A.town_id=A4.addr_id');
    $query->leftJoin('addr', 'A5', 'A.village_id=A5.addr_id');
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('A4', 'name', 'town');
    $query->addField('A5', 'name', 'village');
    //地址级别查询
    if (!empty($form_state['values']['village'])) {//村落级
        $query->condition('A.village_id', $form_state['values']['village'], '=');
    } else if (!empty($form_state['values']['town'])) {//乡镇级
        $query->condition('A.town_id', $form_state['values']['town'], '=');
    } elseif (!empty($form_state['values']['county'])) {//县级
        $query->condition('A.county_id', $form_state['values']['county'], '=');
    } elseif (!empty($form_state['values']['city'])) {//市级
        $query->condition('A.city_id', $form_state['values']['city'], '=');
    } elseif (!empty($form_state['values']['province'])) {//省级
        $query->condition('A.province_id', $form_state['values']['province'], '=');
    }
    $query->fields('PM');
    $query->addField('F', 'farmer_name');
    $query->addField('P', 'plant_name');
    $query->orderBy('PM.timestamp', 'DESC');
    if(!empty($_GET['farmer_id'])){
        $query->condition('PM.farmer_id',$_GET['farmer_id']);
        $form_state['farmer'] = $_GET['farmer_id'];
    }
    if (!empty($form_state['farmer'])) {//
        $query->condition('PM.farmer_id', $form_state['farmer'], '=');
    }
    if ($user_info['role'] != '农户') {
        if (!user_access('view all plant_measure') && user_access('view own plant_measure')) {
            $query->condition('PM.author_uid', $user->uid, '=');
        }
    }
    $query->range(0,20);
    $plant_measure_info = $query->execute()->fetchAll();
    $farmerInfo =['farmer_info'=>$farmer_info,'plant_info'=>$plant_infoInfo,'soil_info'=>$soil_infoInfo,'gift_info'=>$giftInfo,'fertilizer_caseinfo'=>$fertilizer_caseInfo,'revisit_info'=>$revisitInfo,'plant_measure_info'=>$plant_measure_info];

    return $farmerInfo;
}
